package eu.jba.denshijisho.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import eu.jba.denshijisho.R;
import eu.jba.denshijisho.dao.NoteDao;
import eu.jba.denshijisho.entity.CardNote;
import eu.jba.denshijisho.entity.Note;

public class NoteAdapter extends ArrayAdapter<CardNote>{
	
	private Context context;
	private int layoutResourceId;
	private List<CardNote> data;

	public NoteAdapter(Context context,
			int layoutResourceId, List<CardNote> data) {
		super(context, layoutResourceId, data);
		this.context = context;
		this.layoutResourceId = layoutResourceId;
		this.data = data;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent){
		View row = convertView;
		final CardNote cardNote = data.get( position );
		CardNoteHolder cardHolder;
		if( row == null ){
			final LayoutInflater inflater = ((Activity)context).getLayoutInflater();
			cardHolder = new CardNoteHolder();
			row = inflater.inflate( layoutResourceId, parent, false );
			cardHolder.note = (TextView)row.findViewById( R.id.note_text_view_item_row1 );
			row.setTag( cardHolder );
		}else{
			cardHolder = (CardNoteHolder)row.getTag();
		}
		cardHolder.note.setText( getNote( cardNote.getNoteId() ).getNote() );
		
		return row;
	}
	
	private static class CardNoteHolder{
		private TextView note;
	}
	
	private Note getNote(int i){
		final NoteDao noteDao = new NoteDao( context );
		noteDao.open();
		final Note note = noteDao.get( i );
		noteDao.close();
		return note;
	}
}
